/*
 * ModalInternalFrame.java
 *
 * Created on 15 de mayo de 2007, 14:16
 */

package Util_Francisco;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.beans.*;

/**
 *
 * @author  Administrador
 */
public class ModalInternalFrame extends javax.swing.JInternalFrame {
    
    /** Creates new form ModalInternalFrame */
    public ModalInternalFrame(String title, JRootPane rootpane, Component desktop, JOptionPane pane) {
        super(title);
        //create opaque glass pane
        final JPanel glass = new JPanel();
        glass.setOpaque(false);
        
        //Attah mouse listeners
        MouseInputAdapter adapter = new MouseInputAdapter(){};
        glass.addMouseListener(adapter);
        glass.addMouseMotionListener(adapter);
        
         //add in option pane
        getContentPane().add(pane, BorderLayout.CENTER);
        
        //Define close behavior
        PropertyChangeListener pcl =new PropertyChangeListener(){
            public void propertyChange(PropertyChangeEvent event){
                if(isVisible()&&(event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)||
                   event.getPropertyName().equals(JOptionPane.INITIAL_VALUE_PROPERTY))){
                    try{
                        setClosed(true);                        
                    }catch(PropertyVetoException ignored){
                    }
                    ModalInternalFrame.this.setVisible(false);
                    glass.setVisible(false);
                }
            }
        };
        pane.addPropertyChangeListener(pcl);
        //Change frame border
        putClientProperty("JInternalFrame.frameType","optionDialog");
        //Size frame
        
        Dimension size= getPreferredSize();
        Dimension rootSize = desktop.getSize();
        setBounds((rootSize.width- size.width)/2,(rootSize.height-size.height)/2,size.width,size.height);
        desktop.validate();
        try{
            setSelected(true);
        }catch(PropertyVetoException ignored){
        }
        // Add modal internal frame to glass pane
        glass.add(this);
        //Change glass pane to our panel
        rootPane.setGlassPane(glass);
        
        glass.setVisible(true);
        
        //initComponents();
    }
    public void setVisible(boolean value){
            super.setVisible(value);
            if (value){
                startModal();
            }else{
                stopModal();
            }
    }
    private synchronized void startModal(){
        try{
            if(SwingUtilities.isEventDispatchThread()){
                EventQueue theQueue = getToolkit().getSystemEventQueue();
                while(isVisible()){
                    AWTEvent event = theQueue.getNextEvent();
                    Object source = event.getSource();
                    if(event instanceof ActiveEvent){
                        ((ActiveEvent)event).dispatch();
                        
                    }else if(source instanceof Component ){
                            ((Component)source).dispatchEvent(event);
                         }
                        else{
                            System.err.println("Unable to dispatch:"+event);
                        }
                }
            }else{
                while(isVisible()){
                    wait();
                }
            }
        }catch(InterruptedException ignored){
        }        
    }
   
    private synchronized void stopModal(){
        notifyAll();
    }
    
    public static void main(String[]args){
        final JFrame frame = new JFrame("Modal Internal Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        final JDesktopPane desktop =  new JDesktopPane();
        ActionListener showModal = new ActionListener(){
            Integer ZERO = new Integer(0);
            Integer ONE  = new Integer(1);
            public void actionPerformed(ActionEvent e){
                
                //Manually construct an input popup
                JOptionPane optionPane = new JOptionPane("Print", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
                
               JInternalFrame modal = new ModalInternalFrame("Really Modal",frame.getRootPane(), desktop, optionPane);
               modal.setVisible(true);
               
               Object value =optionPane.getValue();
               if(value.equals(ZERO)){
                   System.out.println("Select yes");                   
               }else if(value.equals(ONE)){
                   System.out.println("Select NO");
               }else{
                   System.err.println("Input Error");
               }                
            }            
        } ;
        JInternalFrame internal= new JInternalFrame("Opener");
        desktop.add(internal);
        JButton button = new JButton("Open");
        button.addActionListener(showModal);
        
        Container iContent = internal.getContentPane();
        iContent.add(button, BorderLayout.CENTER);
        internal.setBounds(25,25,200,100);
        internal.setVisible(true);
        
        Container content = frame.getContentPane();
        content.add(desktop, BorderLayout.CENTER);
        frame.setSize(500,300);
        frame.setVisible(true);        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 394, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 274, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
